package com.ruifu.proj.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhzncloud.cloud.common.core.util.R;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.eneity.BaseEntityPlus;
import com.ruifu.common.po.PmPtCondTask;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.proj.mapper.PmPtCondTaskMapper;
import com.ruifu.proj.mapper.PmPtMainMapper;
import com.ruifu.proj.service.PmPtCondTaskService;
import com.ruifu.proj.vo.ptcondtask.PmPtCondTaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author nyr
 * @create 2019/8/13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PmPtCondTaskServiceImpl extends ServiceImpl<PmPtCondTaskMapper, PmPtCondTask> implements PmPtCondTaskService {

    @Autowired(required = false)
    private PmPtCondTaskMapper pmPtCondTaskMapper;

    @Autowired(required = false)
    private PmPtMainMapper pmPtMainMapper;

    /**
     * 查询当前任务的启动条件列表
     *
     * @param pcSId     后序任务id
     * @param pcSStatus 后序任务状态
     * @return
     */
    @Override
    public R findByPcSIdAndPcSStatus(Long pcSId, String pcSStatus) {
        List<PmPtCondTask> pmPtCondTasks = pmPtCondTaskMapper.selectList(Wrappers.<PmPtCondTask>query()
                .lambda()
                .eq(PmPtCondTask::getPcSId, pcSId)
                .eq(PmPtCondTask::getPcSStatus, pcSStatus)
                .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()));
        pmPtCondTasks.forEach(pmPtCondTask -> pmPtCondTask.setPmPtMain(pmPtMainMapper.findById(pmPtCondTask.getPcPId())));
        /*PO转VO*/
        return R.ok(BeanConverter.copyList(pmPtCondTasks, PmPtCondTaskVO.class));
    }
}
